package com.jaynux.app;

import android.app.*;
import android.content.*;
import android.os.*;
import android.view.*;
import android.widget.*;

public class SelectHero extends Activity implements View.OnClickListener {
	
	//측정 결과에 따라 선택할 수 있는 장수를 나타내기 위한 flag
	boolean row1col1Flag = false;
	boolean row1col2Flag = false;
	boolean row1col3Flag = false;
	boolean row2col1Flag = false;
	boolean row2col2Flag = false;
	boolean row2col3Flag = false;
	boolean row3col1Flag = false;
	boolean row3col2Flag = false;
	boolean row3col3Flag = false;
	
	//캐릭터를 선택했을 때  selectImgView에 나타날 이미지 
	int selected_img[][] = {{R.drawable.g_h1,R.drawable.u_h1,R.drawable.j_h1},
			{R.drawable.h_h1, R.drawable.k_h1, R.drawable.ma_cho_h1},
			{R.drawable.yu_bi_h1,R.drawable.jang_bi_h1,R.drawable.hwang_chung_h1}};

	//저작권 없는 장수 얼굴 이미지
	int masking_img[][] = {{R.drawable.gwanu_maskimg_unknow,R.drawable.yeopo_maskimg_unknow,R.drawable.joun_maskimg_unknow},
			{R.drawable.hahudon_maskimg_unknow, R.drawable.taehui_maskimg_unknow, R.drawable.macho_maskimg_unknow},
			{R.drawable.yubi_maskimg_unknow,R.drawable.jangbi_maskimg_unknow,R.drawable.hwangchung_maskimg_unknow}};
	
	//저작권 있는 장수 얼굴 이미지
	int masking_img2[][] = {{R.drawable.gwanu_maskimg,R.drawable.yeopo_maskimg,R.drawable.joun_maskimg},
			{R.drawable.hahudon_maskimg, R.drawable.taehui_maskimg, R.drawable.macho_maskimg},
			{R.drawable.yubi_maskimg,R.drawable.jangbi_maskimg,R.drawable.hwangchung_maskimg}};
	
	//각 행 열의 버튼 
	ImageButton row1col1;
	ImageButton row1col2;
	ImageButton row1col3;
	ImageButton row2col1;
	ImageButton row2col2;
	ImageButton row2col3;
	ImageButton row3col1;
	ImageButton row3col2;
	ImageButton row3col3;
	
	Button cancelBtn;
	Button selectBtn;
	
	//선택된 영웅의 정보를 표시하는 뷰
	TextView selectedHeroInfo;
	//선택된 영웅의 옵션을 표시하는 뷰
	TextView selectedHeroOption;
	
	ImageView selectImgView;	//선택된 장수를 보여주는 뷰
	LinearLayout heroBtnView;	//장수를 선택 하는 버튼들을 담은 레이아웃
	
	Hero hero[][] = new Hero[3][3];
	
	// 선택 한 장수의 옵션
	String name;
	int ag;
	int speed;
	int booster;
	int damage;
	
	Intent i;
	
	//현재 장수가 선택되있는 상태인지를 나타내는 flag
	boolean selectFlag = false;
	
	//프레퍼런스 객체
	SharedPreferences mPref;
	SharedPreferences.Editor mEdit;
	boolean mPreferences = false;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.selecthero);
		
		row1col1 = (ImageButton)findViewById(R.id.row1col1);
		row1col2 = (ImageButton)findViewById(R.id.row1col2);
		row1col3 = (ImageButton)findViewById(R.id.row1col3);
		row2col1 = (ImageButton)findViewById(R.id.row2col1);
		row2col2 = (ImageButton)findViewById(R.id.row2col2);
		row2col3 = (ImageButton)findViewById(R.id.row2col3);
		row3col1 = (ImageButton)findViewById(R.id.row3col1);
		row3col2 = (ImageButton)findViewById(R.id.row3col2);
		row3col3 = (ImageButton)findViewById(R.id.row3col3);
		
		cancelBtn = (Button)findViewById(R.id.cancel);
		selectBtn = (Button)findViewById(R.id.select);
		
		selectImgView = (ImageView)findViewById(R.id.selectImgView);
		heroBtnView = (LinearLayout)findViewById(R.id.heorButtonView);
		
		selectedHeroInfo = (TextView)findViewById(R.id.selectedHeroInfoView);
		selectedHeroOption = (TextView)findViewById(R.id.selectedHeroOptionView);
		
		row1col1.setOnClickListener(this);
		row1col2.setOnClickListener(this);
		row1col3.setOnClickListener(this);
		row2col1.setOnClickListener(this);
		row2col2.setOnClickListener(this);
		row2col3.setOnClickListener(this);
		row3col1.setOnClickListener(this);
		row3col2.setOnClickListener(this);
		row3col3.setOnClickListener(this);	
		
		cancelBtn.setOnClickListener(this);
		selectBtn.setOnClickListener(this);
		
		hero[0][0] = new Hero("관우",-10,0,1,5);
		hero[0][1] = new Hero("여포",0,0,20,6);
		hero[0][2] = new Hero("조운",-3,3,3,4);
		hero[1][0] = new Hero("하후돈",0,1,10,7);
		hero[1][1] = new Hero("태희",0,0,0,0);
		hero[1][2] = new Hero("마초",-1,1,3,7);
		hero[2][0] = new Hero("유비",0,15,0,0);
		hero[2][1] = new Hero("장비",-2,1,4,6);
		hero[2][2] = new Hero("황충",-2,2,2,4);
		
		/*
		hero[0][0] = new Hero("관우",0,0,5,6);
		hero[0][1] = new Hero("여포",0,0,5,6);
		hero[0][2] = new Hero("조운",0,0,5,6);
		hero[1][0] = new Hero("하후돈",0,0,5,6);
		hero[1][1] = new Hero("태희",0,0,5,6);
		hero[1][2] = new Hero("마초",0,0,5,6);
		hero[2][0] = new Hero("유비",0,0,5,6);
		hero[2][1] = new Hero("장비",0,0,5,6);
		hero[2][2] = new Hero("황충",0,0,5,6);
		*/
		//전송되어진 인텐트 값을 받아온다.
		i = getIntent();
		
		mPref = getSharedPreferences("preftest", 0);
		mEdit = mPref.edit();
		mEdit.putBoolean("taehui_prefer_info", true);
		mEdit.commit();
		//장수의 오픈 여부를 인텐트값을 통해서 판단해주는 함수이다.
		setSelectableHero();
		
		//조건을 기준으로 버튼이미지를 표시한다.
		setBtnImg();
	}
	
	public class Hero {
		Hero(String arName, int arAg, int arSpeed, int arBooster, int arDamage) {
			name = arName;
			ag = arAg;
			speed = arSpeed;
			booster = arBooster;
			damage = arDamage;
		}
		
		String name;
		int ag;
		int speed;
		int booster;
		int damage;
	}
	
	//현재 선택된 장수의 옵션을 적용
	public void setHeroOption(int row, int col) {
		name = hero[row][col].name;
		ag = hero[row][col].ag;
		speed = hero[row][col].speed;
		booster = hero[row][col].booster;
		damage = hero[row][col].damage;
		selectFlag = true;
	}
	
	//측정 결과를 바탕으로 선택할 수 있는 장수의 flag를 true로 세팅해 준다
	public void setSelectableHero() {
		mPreferences = i.getBooleanExtra("preferences", false);
		if(mPreferences){
			row1col1Flag = mPref.getBoolean("gwanu_prefer_info", false);
			row1col2Flag = mPref.getBoolean("yeopo_prefer_info", false);
			row1col3Flag = mPref.getBoolean("joun_prefer_info", false);
			row2col1Flag = mPref.getBoolean("hahudon_prefer_info", false);
			row2col2Flag = mPref.getBoolean("taehui_prefer_info", false);
			row2col3Flag = mPref.getBoolean("macho_prefer_info", false);
			row3col1Flag = mPref.getBoolean("yubi_prefer_info", false);
			row3col2Flag = mPref.getBoolean("jangbi_prefer_info", false);
			row3col3Flag = mPref.getBoolean("hwangchung_prefer_info", false);
		}
		else{
			row1col1Flag = i.getBooleanExtra("row1col1",false);
			row1col2Flag = i.getBooleanExtra("row1col2",false);
			row1col3Flag = i.getBooleanExtra("row1col3",false);
			row2col1Flag = i.getBooleanExtra("row2col1",false);
			row2col2Flag = i.getBooleanExtra("row2col2",true);
			row2col3Flag = i.getBooleanExtra("row2col3",false);
			row3col1Flag = i.getBooleanExtra("row3col1",false);
			row3col2Flag = i.getBooleanExtra("row3col2",false);
			row3col3Flag = i.getBooleanExtra("row3col3",false);
		}
	}
	
	//flag에 따라 선택 가능한 장수는 원래 이미지를 불가능한 장수는 물음표 이미지를 세팅한다
	public void setBtnImg() {
		if(row1col1Flag) {
			row1col1.setImageResource(selected_img[0][0]);
		} else {
			row1col1.setImageResource(R.drawable.question);
		}
		
		if(row1col2Flag) {
			row1col2.setImageResource(selected_img[0][1]);
		} else {
			row1col2.setImageResource(R.drawable.question);
		}
		
		if(row1col3Flag) {
			row1col3.setImageResource(selected_img[0][2]);
		} else {
			row1col3.setImageResource(R.drawable.question);
		}
		
		if(row2col1Flag) {
			row2col1.setImageResource(selected_img[1][0]);
		} else {
			row2col1.setImageResource(R.drawable.question);
		}
		
		if(row2col2Flag) {
			row2col2.setImageResource(selected_img[1][1]);
		} else {
			row2col2.setImageResource(R.drawable.question);
		}
		
		if(row2col3Flag) {
			row2col3.setImageResource(selected_img[1][2]);
		} else {
			row2col3.setImageResource(R.drawable.question);
		}
		
		if(row3col1Flag) {
			row3col1.setImageResource(selected_img[2][0]);
		} else {
			row3col1.setImageResource(R.drawable.question);
		}
		
		if(row3col2Flag) {
			row3col2.setImageResource(selected_img[2][1]);
		} else {
			row3col2.setImageResource(R.drawable.question);
		}
		
		if(row3col3Flag) {
			row3col3.setImageResource(selected_img[2][2]);
		} else {
			row3col3.setImageResource(R.drawable.question);
		}
			
	}
	
	public void setting(int row, int col) {
		selectImgView.setVisibility(View.VISIBLE);
		heroBtnView.setVisibility(View.INVISIBLE);
		selectImgView.setImageResource(masking_img2[row][col]);
		setHeroOption(row, col);
	}
	
	public void setOptionTextView() {
		String option_ag ;
		String option_speed;
		String option_booster;
		String option_damage;
		if(ag >=0)
			option_ag ="+"+ag;
		else
			option_ag =""+ag;
		if(speed >=0) 
			option_speed="+"+speed;
		else
			option_speed=""+speed;
		if(booster >=0)
			option_booster ="+"+booster;
		else
			option_booster =""+booster;
		if(damage >=0)
			option_damage ="+"+damage;
		else
			option_damage =""+damage;
			
		String str ="Option\n"+"Tick : "+option_ag+"\nSpeed : "+option_speed+"\nBooster : "+option_booster+
		"\nDamage : "+option_damage;
		selectedHeroOption.setText(str);
	}
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(row1col1.equals(v)) {
			if(row1col1Flag) {
				setting(0,0);
				selectedHeroInfo.setText(R.string.g);	//선택된 장수의 정보와 옵션을 쓴다
				setOptionTextView();
			}
			else  
				Toast.makeText(SelectHero.this, "오픈 조건이 충족되지 않았습니다", Toast.LENGTH_SHORT).show();
		} else if (row1col2.equals(v)) {
			if(row1col2Flag) {
				setting(0,1);
				selectedHeroInfo.setText(R.string.u);	//선택된 장수의 정보와 옵션을 쓴다
				setOptionTextView();
			}
			else  
				Toast.makeText(SelectHero.this, "오픈 조건이 충족되지 않았습니다", Toast.LENGTH_SHORT).show();
		} else if (row1col3.equals(v)) {
			if(row1col3Flag) {				
				setting(0,2);
				selectedHeroInfo.setText(R.string.j);	//선택된 장수의 정보와 옵션을 쓴다
				setOptionTextView();
			}
			else  
				Toast.makeText(SelectHero.this, "오픈 조건이 충족되지 않았습니다", Toast.LENGTH_SHORT).show();
		} else if (row2col1.equals(v)) {
			if(row2col1Flag) {
				setting(1,0);
				selectedHeroInfo.setText(R.string.h);	//선택된 장수의 정보와 옵션을 쓴다
				setOptionTextView();
			}
			else  
				Toast.makeText(SelectHero.this, "오픈 조건이 충족되지 않았습니다", Toast.LENGTH_SHORT).show();
		} else if (row2col2.equals(v)) {
			if(row2col2Flag) {
				setting(1,1);
				selectedHeroInfo.setText(R.string.k);	//선택된 장수의 정보와 옵션을 쓴다
				setOptionTextView();
			}
			else  
				Toast.makeText(SelectHero.this, "오픈 조건이 충족되지 않았습니다", Toast.LENGTH_SHORT).show();
		} else if (row2col3.equals(v)) {
			if(row2col3Flag) {
				setting(1,2);
				selectedHeroInfo.setText(R.string.ma_cho_description);	//선택된 장수의 정보와 옵션을 쓴다
				setOptionTextView();
			}
			else  
				Toast.makeText(SelectHero.this, "오픈 조건이 충족되지 않았습니다", Toast.LENGTH_SHORT).show();
		} else if (row3col1.equals(v)) {
			if(row3col1Flag) {
				setting(2,0);
				selectedHeroInfo.setText(R.string.yu_bi_description);	//선택된 장수의 정보와 옵션을 쓴다
				setOptionTextView();
			}
			else  
				Toast.makeText(SelectHero.this, "오픈 조건이 충족되지 않았습니다", Toast.LENGTH_SHORT).show();
		} else if (row3col2.equals(v)) {
			if(row3col2Flag) {
				setting(2,1);
				selectedHeroInfo.setText(R.string.jang_bi_description);	//선택된 장수의 정보와 옵션을 쓴다
				setOptionTextView();
			}
			else  
				Toast.makeText(SelectHero.this, "오픈 조건이 충족되지 않았습니다", Toast.LENGTH_SHORT).show();
		} else if (row3col3.equals(v)) {
			if(row3col3Flag) {
				setting(2,2);
				selectedHeroInfo.setText(R.string.hwang_chung_description);	//선택된 장수의 정보와 옵션을 쓴다
				setOptionTextView();
			}
			else  
				Toast.makeText(SelectHero.this, "오픈 조건이 충족되지 않았습니다", Toast.LENGTH_SHORT).show();
		} else if (cancelBtn.equals(v)) {
			selectImgView.setVisibility(View.INVISIBLE);
			heroBtnView.setVisibility(View.VISIBLE);
			selectedHeroInfo.setText("");
			selectedHeroOption.setText("");
			selectFlag = false;
		} else if (selectBtn.equals(v)) {
			if(selectFlag) {
				Intent intent = new Intent();
				intent.putExtra("name", name);
				intent.putExtra("ag", ag);
				intent.putExtra("speed", speed);
				intent.putExtra("booster", booster);
				intent.putExtra("damage", damage);
				
				setResult(RESULT_OK, intent);
				finish();
			} else {
				Toast.makeText(SelectHero.this, "캐릭터를 선택해 주세요", Toast.LENGTH_SHORT).show();
			}
		}
	}

}
